home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0208_Checking a string in a Combo Box.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-03-04  |  1.4 KB  |  66 lines

  1.  
  2. { method #1 }
  3.  
  4. unit Unit1;
  5.  
  6. interface
  7.  
  8. uses
  9.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  10.   StdCtrls;
  11.  
  12. type
  13.   TForm1 = class(TForm)
  14.     ComboBox1: TComboBox;
  15.     Edit1: TEdit;
  16.     Edit2: TEdit;
  17.     procedure Edit1Change(Sender: TObject);
  18.   private
  19.     { Private declarations }
  20.   public
  21.     { Public declarations }
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.DFM}
  30.  
  31. function ComboBox_Item_Exists(ComboBox: tComboBox; str: string): Integer;
  32. var i: Integer;
  33. begin
  34.          if ComboBox.Items.Count = 0 then
  35.          // ComboBox is empty
  36.          begin
  37.               Result := -1;  // not found
  38.               Exit;
  39.          end else
  40.                   for i := 0 to ComboBox.Items.Count -1 do
  41.                   begin
  42.                        if ComboBox.Items[i] = str then
  43.                        begin
  44.                             Result := i; // eureka
  45.                             // str at pos i in ComboBox
  46.                             Exit; // look no further
  47.                        end
  48.                        else Result := -1; // not found
  49.                   end;
  50. end;
  51.  
  52. procedure TForm1.Edit1Change(Sender: TObject);
  53. begin
  54.      ComboBox1.ItemIndex := ComboBox_Item_Exists(ComboBox1, Edit1.Text);
  55. end;
  56.  
  57. end.
  58.  
  59. What do you think about this replace ?
  60.  
  61. function ComboBox_Item_Exists(ComboBox: tComboBox; str: string): Integer;
  62. begin
  63.    Result := ComboBox.Items.IndexOf( Str );
  64. end;
  65.  
  66.